﻿package com.bizmlib.ui.display
{
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	
	public class InteractiveObjectContainer extends MovieClip
	{
		private static var EVENTS:Array = new Array(
			MouseEvent.CLICK,
			MouseEvent.DOUBLE_CLICK,
			MouseEvent.MOUSE_DOWN,
			MouseEvent.MOUSE_MOVE,
			MouseEvent.MOUSE_OUT,
			MouseEvent.MOUSE_OVER,
			MouseEvent.MOUSE_UP,
			MouseEvent.MOUSE_WHEEL,
			MouseEvent.ROLL_OUT,
			MouseEvent.ROLL_OVER,
			FocusEvent.FOCUS_IN,
			FocusEvent.FOCUS_OUT,
			FocusEvent.KEY_FOCUS_CHANGE,
			FocusEvent.MOUSE_FOCUS_CHANGE,
			KeyboardEvent.KEY_DOWN,
			KeyboardEvent.KEY_UP,
			TextEvent.TEXT_INPUT
		);
													
		private var __interactionEnabled:Boolean;
		
		private var __view:DisplayObject;
		
		
		public function InteractiveObjectContainer()
		{
			init();
		}
		
		
		public function set interactionEnabled(value:Boolean):void
		{
			__interactionEnabled = value;
			updateInteractionEnabled();
		}
		public function get interactionEnabled():Boolean
		{
			return __interactionEnabled;
		}
		
		public function set view(value:DisplayObject):void
		{
			if(view)
			{
				removeChild(view);
			}
			
			__view = value;
			if(view)
			{
				addChild(view);
				interactionEnabled = interactionEnabled;
			}
		}
		public function get view():DisplayObject
		{
			return __view;
		}
		
		
		private function init():void
		{
			mouseEnabled = false;
			tabEnabled = false;
			
			interactionEnabled = true;
			
			for(var i:Number=0; i<EVENTS.length; i++)
			{
				this.addEventListener(EVENTS[i], handleCapturingEvent, true, Number.POSITIVE_INFINITY);
			}
		}
		private function updateInteractionEnabled():void
		{
			mouseChildren = interactionEnabled;
			tabChildren = interactionEnabled;
		}
		
		private function handleCapturingEvent(event:Event):void
		{
			if(!interactionEnabled)
			{
				event.stopImmediatePropagation();
			}
		}
		
		
		override public function toString():String
		{
			return "[InteractiveObjectContainer]";
		}
	}
}